/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.repository;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class Identifier {

    private static final String ID_PREFIX = "oai:aleph.mzk.cz:";
    private static final String BASE_SYSNO_SEPARATOR = "-";
    private static final int SYSNO_LENGTH = 9;
    private MetadataBase base;
    private String sysno;
    private String id = null; //id as String constructed from prefix, base, sysno

    public Identifier(MetadataBase base, String sysno) {
        this.base = base;
        this.sysno = sysno;
        if (sysno.length() != SYSNO_LENGTH) {
            throw new IllegalArgumentException("sysno is of incorrect length - " + sysno.length() + " instead of " + SYSNO_LENGTH);
        }
    }

    @Override
    public String toString() {
        if (id != null) {
            return id;
        } else {
            StringBuilder builder = new StringBuilder();
            builder.append(ID_PREFIX);
            builder.append(base.toString().toUpperCase());//NOTE: toString() gives lower case but identifiers now contain base in upper case
            builder.append(BASE_SYSNO_SEPARATOR);
            builder.append(sysno);//TODO: 001041863 nebo 1041863 ?
            return builder.toString();
        }
    }

    /**
     * @param id String tested for being valid identifier
     * @param base base of the identifier
     * @return true if id is valid identifier within the base
     */
    public static boolean isIdentifier(String id, MetadataBase base) {
        if (id == null) {
            return false;
        }

        int from;
        int to;
        int prefixLength = ID_PREFIX.length();
        int baseLength = base.toString().length();
        int separatorLength = BASE_SYSNO_SEPARATOR.length();

        //length
        int length = prefixLength + baseLength + separatorLength + SYSNO_LENGTH;
        if (id.length() != length) {
            return false;
        }

        //"oai:aleph.mzk.cz:"
        from = 0;
        to = prefixLength;
        String prefixSubstr = id.substring(from, to);
        if (!prefixSubstr.equals(ID_PREFIX)) {
            return false;
        }

        //"MZK03"
        from = prefixLength;
        to = prefixLength + baseLength;
        String baseSubstr = id.substring(from, to);
        if (!baseSubstr.equals(base.toString().toUpperCase())) {//NOTE: toString() gives lower case but identifiers now contain base in upper case
            return false;
        }

        //"-"
        from = prefixLength + baseLength;
        to = prefixLength + baseLength + separatorLength;
        String separatorSubstr = id.substring(from, to);
        if (!separatorSubstr.equals(BASE_SYSNO_SEPARATOR)) {
            return false;
        }

//        //"001024481"
//        from = prefixLength + baseLength + separatorLength;
//        to = id.length();
//        String sysnoSubstr = id.substring(from, to);
//        //NOTE: not checking if sysno contains just numbers

        return true;
    }
}
